<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class News_model extends MY_Model {
	
	protected $news;
	protected $categories_news;
	
	function __construct(){
		parent::__construct();
	}
	
	function table_name(){
		return "news";
	}
	
	function insert_new($data){
		$_category_id = $data['category_id'];
		$_new_id = $this->save($data);
		$this->save(array('category_id'=>$_category_id,'new_id'=>$_new_id),'categories_news');
		return true;
	}
	
	function update_new($data,$new_id){
		$attributes = $this->delete_image_file($data,$new_id);
		$this->update($attributes,array('new_id'=>$new_id),'categories_news');
		return $this->update($attributes,array('new_id'=>$new_id));
	}
	
	function delete_new($new_id){
		$this->delete_image_file(false,$new_id,false);
		$this->delete(array('new_id'=>$new_id),'categories_news');
		return $this->delete(array('new_id'=>$new_id));
	}
	
	function delete_image_file($data = false,$new_id,$is_delete = false){
		$files = $this->db->select('image')->from('news')->where('new_id',$new_id)->get()->result_array();
		foreach ($files[0] as $k=>$v){
			if (is_array($data) && count($data) > 0 && array_key_exists($k.'_delete',$data)){
				if (file_exists($v)){
					unlink($v);
				}
				$data[$k]="";
			}else if ($is_delete){
				if (file_exists($v)){
					unlink($v);
				}
			}
		}
		return $data;
	}
	
	function get_news($filters = array(),$counting = false){
		if (isset($this->CI->cache)){
			$caching = true;
			$cache_key = 'ci--NEWS_'.md5(serialize($filters));
			if ($counting){
				$cache_key = 'ci--NEWS_COUNT_'.md5(serialize($filters));
			}
			if ($return = $this->cache->file->get($cache_key)) {
				return ($return == 'empty_cache') ? FALSE : $return;
			}
		}else{
			$caching = false;
		}
		$this->db->select('news.*,cn.category_id as category_id,cc.name as category_name,cc.alias AS category_alias')
				->join('categories_news as cn','cn.new_id = news.new_id','left')
				->join('categories as cc','cc.category_id = cn.category_id','left')
				->from('news');
		
		if (isset($filters['new_id'])){
			$this->db->where('news.new_id',$filters['new_id']);
		}
		
		if (isset($filters['category_id'])){
			$this->db->where('(cc.category_id ='.$filters['category_id'].' OR cc.parent_id ='.$filters['category_id'].')');
		}
		
		if (isset($filters['new_category'])){
			$this->db->where('cc.category_id',$filters['new_category']);
		}
		
		if (isset($filters['status'])){
			$this->db->where('news.status',$filters['status']);
		}
		
		if (isset($filters['featured'])){
			$this->db->where('news.featured',1);
		}
		
		if (isset($filters['is_hot'])){
			$this->db->where('news.is_hot',1);
		}
		
		if (isset($filters['other_new_id'])){
			$this->db->where('news.new_id !=',$filters['other_new_id']);
		}
		
		if (isset($filters['alias'])){
			$this->db->where('news.alias',$filters['alias']);
		}
		
		if (isset($filters['limit'])) {
			$offset = (isset($filters['page'])) ? $filters['page'] : 0;
			$this->db->limit($filters['limit'], $offset);
		}
		
		$this->db->order_by('news.new_id','DESC');
		
		$result = $this->db->get();
		if($counting !=false){
			$rows = $result->num_rows();
			$result->free_result();
			
			if ($caching == TRUE) {
				$this->CI->cache->file->save($cache_key, $rows, (5*60));	
			}
			
			return $rows;
		}
		if ($result->num_rows() == 0) {
			return false;
		}
		$news = array();
		foreach ($result->result_array() as $row ){
			$row['link'] = create_url('news/'.$row['category_alias'].'/'.$row['alias'],array('rewrite'=>true));
			$row['date'] = fdate($row['created'],"%A, %d/%m/%Y - %H:%M");
			$news[] = $row;
		}
		if ($caching){
			$this->CI->cache->file->save($cache_key,$news);
		}
		$result->free_result();
		return $news;
	}
	
	function get_new($newId){
		if (!isset($this->news[$newId])){
			$new = $this->get_news(array('new_id'=>$newId));
			$this->news[$newId] = $new;
		}else{
			$new = $this->news[$newId];
		}
		if (empty($new)) {
			return false;
		}
		return $new[0];
	}
	
	function get_count($filter = array()){
		return $this->get_news($filter,true);
	}
	
	function get_new_by_alias($new_alias){
		$new = $this->get_news(array('alias'=>$new_alias),false);
		if ($new){
			return $new[0];
		}
		return false;
	}
	
	function get_categories($filters = array(),$counting = false,$treerecurse = true){
		$this->CI->load->model('categories/categories_model');
		$filters = array_merge(array('module'=>'news'),$filters);
		$cactegory_news = $this->categories_model->get_categories($filters,$counting);
		if ($counting){
			return $cactegory_news;
		}
		if ($treerecurse){
			$list_categories = array();
			if (is_array($cactegory_news) && count($cactegory_news)> 0){
				$children = array();
				foreach ($cactegory_news as $v){
					$v['id'] = $v['category_id'];
					$pt = $v['parent_id'];
					$list = @$children[$pt] ? $children[$pt] : array();
					array_push( $list, $v );
					$children[$pt] = $list;
				}
				$list_categories = treerecurse(0,'',array(), $children,99999);
			}
			return $list_categories;
		}else{
			if ($cactegory_news){
				return $cactegory_news;
			}
		}
		return false;
	}
	
	function get_count_categories($filters = array()){
		return $this->get_categories($filters,true);
	}
	
	function get_categories_options($filters=array()){
		$categories_options = array();
		$categories = $this->get_categories($filters);
		if (is_array($categories) && count($categories)){
			foreach ($categories as $category){
				$categories_options[$category['category_id']] = $category['name'];
			}
		}

		return $categories_options;
	}
	
	function insert_category_new($categoryId,$newsId){
		$fields = array('category_id'=>$categoryId,'new_id'=>$newsId);
		$this->db->insert('categories_news',$fields);
	}
	
	function delete_category($category_id){
		$this->db->where('category_id',$category_id);
		$this->db->delete('categories_news');
		$this->load->model('categories/categories_model');
		$this->categories_model->delete_category($category_id);
		if (isset($this->CI->cache)) {
			$this->CI->cache->file->clean();
		}
		return true;
	}
}



















